﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Extensil.Common.CategoryHandler.Discovery
{
    public abstract class BaseProbeHandler
    {
        public delegate void ReportEvent(string message);
        ReportEvent _evt = null;
        public void SetReportEvent(ReportEvent evt)
        {
            _evt = evt;
        }
        public void ReportStatus(string message)
        {
            if(_evt != null)
                _evt(message);
        }
        public abstract string[] Keys
        {
            get;
        }
        List<ProbeParam> _params;
        public List<ProbeParam> Parameters
        {
            get
            {
                if(_params != null)
                    return _params;
                _params = new List<ProbeParam>();
                foreach(string param in Keys)
                    _params.Add(new ProbeParam(param));
                return _params;
            }
        }
        public bool ValidateParam(ProbeParamList paramList, out string validationMessage)
        {
            try
            {
                validationMessage = "Valid parametes";
                return this.ValidateParam(paramList);
            }
            catch(Exception exc)
            {
                validationMessage = exc.Message;
                return false;
            }
        }
        public virtual bool ValidateParam(ProbeParamList paramList)
        {
            return true;
        }
        public void ConnectProbe(ProbeParamList paramList)
        {
            string message=String.Empty;
            if(ValidateParam(paramList, out message))
                this.Connect(paramList);
            else
                throw new Exception(message);
            return;
        }
        public abstract void Connect(ProbeParamList paramList);
        public DiscoveryResultList RunDiscovery()
        {
            DiscoveryResultList result = this.Search();
            foreach(DiscoveryResult dr in result)
                dr.ProbeHandler = this;
            return result;
        }
        public abstract DiscoveryResultList Search();
        public DiscoveryResultList RunDiscovery(DateTime modifiedOrAddedSince)
        {
            DiscoveryResultList result = this.Search(modifiedOrAddedSince);
            foreach(DiscoveryResult dr in result)
                dr.ProbeHandler = this;
            return result;
        }
        public abstract DiscoveryResultList Search(DateTime modifiedOrAddedSince);
        public abstract string DownloadPlugin(string path);
    }
}
